package com.synergy.stockflow.team.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.team.entity.TeamInfo;
import com.synergy.stockflow.team.service.TeamInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/team-info")

public class TeamInfoController {


    @Autowired
    TeamInfoService teamInfoService;

    @GetMapping("/list")
    public Result<Page<TeamInfo>> list(
            // 接收前端传递的搜索参数
            String teamType,
            String teamId,
            String teamName,
            // 接收前端传递的分页参数
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size
    ){
        // 构建查询条件
        QueryWrapper<TeamInfo> queryWrapper = new QueryWrapper<>();
        // 班组类型不为空时添加筛选条件
        if (teamType != null && !teamType.isEmpty()) {
            queryWrapper.eq("team_type", teamType);
        }
        // 班组编号不为空时添加模糊查询条件
        if (teamId != null && !teamId.isEmpty()) {
            queryWrapper.like("team_id", teamId);
        }
        // 班组名称不为空时添加模糊查询条件
        if (teamName != null && !teamName.isEmpty()) {
            queryWrapper.like("team_name", teamName);
        }
        Page<TeamInfo> page = new Page<TeamInfo>(current,size);
        teamInfoService.page(page,queryWrapper);
        return Result.success(page);
    }
    @PutMapping ("/update")
    public Result<String> update(@RequestBody TeamInfo teamInfo) {
        boolean update = teamInfoService.updateById(teamInfo);
    if(update){
            return Result.success("修改成功",null);
        }else {
            return Result.fail("修改失败,请重试",null);
        }
    }
    @PostMapping("/add")
    public Result<String> add(@RequestBody TeamInfo teamInfo) {
        boolean save = teamInfoService.save(teamInfo);
        if (save) {
            return Result.success("新增成功");
        } else {
            return Result.fail("新增失败", null);
        }
    }
}
